package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.AreaEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.AreaService;
import org.adream.account.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AreaApiController {
	
	@Autowired
	private AreaService areaService;
	
	/**
	 * 查询全部
	 * @return
	 */
	@RequestMapping(value = "/area", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAreas() {
		List<AreaEntity> areas = areaService.queryAreas();
		if(Utils.isEmptyCollection(areas)) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "未查询到相应数据", null);
		}
		return new ResultModel<List<AreaEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, areas);
	}
	
	/**
	 * 根据父级id查询
	 * @param pid 父级id
	 * @return
	 */
	@RequestMapping(value = "/area/pid/{pid}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAreasByPid(@PathVariable("pid") Integer pid) {
		if(pid == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		List<AreaEntity> areas = areaService.queryAreasByPid(pid);
		if(Utils.isEmptyCollection(areas)) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "未查询到相应数据", null);
		}
		return new ResultModel<List<AreaEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, areas);
	}
	
	/**
	 * 根据id查询
	 * @param areaId
	 * @return
	 */
	@RequestMapping(value = "/area/{areaId}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAreaById(@PathVariable("areaId") Integer areaId) {
		if(areaId == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		AreaEntity area = areaService.queryAreaById(areaId);
		if(area == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "未查询到相应数据", null);
		}
		return new ResultModel<AreaEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, area);
	}
}